package com.niuniu.order_consumer.service.impl;

import com.niuniu.order_consumer.exception.NotReportedException;
import com.niuniu.order_consumer.exception.ReportedException;

import com.niuniu.order_consumer.mapper.OrderMapper;
import com.niuniu.order_consumer.pojo.entity.Order;
import com.niuniu.order_consumer.pojo.entity.OrderMessage;
import com.niuniu.order_consumer.service.OrderService;
import com.niuniu.order_consumer.service.RoomService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Meng
 * 2022/12/15
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RoomService roomService;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public Order processOrder(OrderMessage message) {
        Order order = message.getOrder();
        try{
            Integer roomId;
            Integer groupId = order.getGroupId();
            //检查是否有成功订单
            if(groupId==null){
                roomId = (Integer) redisTemplate.opsForValue().get("success_student:"+order.getUid());
                if(roomId!=null){
                    order.setResultContent("学生已有订单选到宿舍"+roomId);
                    throw new ReportedException("学生已有订单选到宿舍"+roomId);
                }
            }else{
                roomId = (Integer) redisTemplate.opsForValue().get("success_group:"+groupId);
                if(roomId!=null){
                    order.setResultContent("团队已有订单选到宿舍"+roomId);
                    throw new ReportedException("团队已有订单选到宿舍"+roomId);
                }
            }

            List<Integer> uidList = message.getUidList();
            //分配宿舍及床位

            roomId = roomService.allocateRoom(order.getBuildingId(),message.getGender(),message.getClassId(),uidList);



            //更新订单信息
            order.setRoomId(roomId);
            order.setStatus(1);
            order.setResultContent("成功选到"+roomId);
            //更新redis缓存
            if(groupId==null){
                redisTemplate.opsForValue().set("success_student:"+order.getUid(),roomId);
            }else{
                redisTemplate.opsForValue().set("success_group:"+groupId,roomId);
            }
        }catch (Exception e) {
            order.setStatus(2);
            if(e instanceof ReportedException){
                order.setResultContent(e.getMessage());
            }else if(e instanceof NotReportedException){
                order.setResultContent("没有空闲房间！！！");
                System.out.println(e.getMessage());
            }else{
                order.setResultContent("没有空闲房间！！！");
                e.printStackTrace();
            }
        }finally {
            //保存订单
            order.setFinishTime((int)(System.currentTimeMillis()/1000));
            if(order.getGroupId()==null){
                order.setGroupId(0);
            }
            try {
                orderMapper.insertOrder(order);
            }catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return order;
    }
}
